package com.aaa.forum.interceptor;

import com.aaa.forum.config.AppConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class AppInterceptorConfigurer implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加登录拦截器
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")                 // 拦截所有请求
                .excludePathPatterns("/sign-in.html")   // 排除登录页面
                .excludePathPatterns("/sign-up.html")   // 排除注册页面
                .excludePathPatterns("/user/login")     // 排除登录接口
                .excludePathPatterns("/user/register")  // 排除注册接口
                .excludePathPatterns("/user/logout")    // 排除退出接口
                .excludePathPatterns("/swagger*/**")    // 排除登录swagger下所有
                .excludePathPatterns("/v3*/**")         // 排除登录v3下所有,与swagger相关
                .excludePathPatterns("/dist/**")        // 排除所有静态文件
                .excludePathPatterns("/image/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/**.ico")
                .excludePathPatterns("/js/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置本地资源映射路径，将url与物理路径进行关联
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + AppConfig.IMAGE_PATH + "/");
    }
}
